﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace DH.Share.Comm.DataHelp
{
    public static class CustomerExpend
    {
        #region 返回表的库名全称
        public static string TbFullName(this string tbname)
        {
            /*       if (!string.IsNullOrEmpty(tbname) && ClsFullName.DbName != null)
                   {
                       var v_dbname = ClsFullName.DbName.Select($"info_tbname='{tbname}'");
                       if (v_dbname.Length == 0)
                       {
                           return $"[UDataBase].[dbo].{tbname}";
                       }
                       else if (v_dbname.Length == 1)
                       {
                           return $"[{v_dbname[0]["info_dbname"].ToString()}].[dbo].[{tbname}]";
                       }
                       else if (v_dbname.Length > 1)
                       {
                           return $"[{ClsLogininfo.DataBaseName}].[dbo].[{tbname}]";
                       }
                       else
                       {
                           return string.Empty;
                       }
                   }
                   else
                   {
                       return string.Empty;
                   }*/
            return "";
        }
        #endregion
        #region 加密字符串
        public static string EncryptAes(this string plainText)
        {
            if (string.IsNullOrEmpty(plainText))
            {
                return string.Empty;
            }
            string myKey = "1234567892323456";
            string myIv = "abcdefghijklmnop";
            Byte[] key_Array = System.Text.Encoding.UTF8.GetBytes(myKey);
            Byte[] iv_Array = System.Text.Encoding.UTF8.GetBytes(myIv);
            // Check arguments.
            if (plainText == null || plainText.Length <= 0)
                throw new ArgumentNullException(nameof(plainText));
            Byte[] toEncryptArray = System.Text.Encoding.UTF8.GetBytes(plainText);
            var rijndael = new RijndaelManaged()
            { Key = key_Array, Mode = System.Security.Cryptography.CipherMode.ECB, Padding = System.Security.Cryptography.PaddingMode.PKCS7, IV = iv_Array };
            System.Security.Cryptography.ICryptoTransform cTransform = rijndael.CreateEncryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            rijndael.Dispose();
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
        #endregion

        #region 解密字符串
        public static string DecryptAes(this string plainText)
        {
            if (string.IsNullOrEmpty(plainText))
            {
                return string.Empty;
            }
            string myKey = "1234567892323456";
            string myIv = "abcdefghijklmnop";
            Byte[] key_Array = System.Text.Encoding.UTF8.GetBytes(myKey);
            Byte[] iv_Array = System.Text.Encoding.UTF8.GetBytes(myIv);
            // Check arguments.
            if (plainText == null || plainText.Length <= 0)
                throw new ArgumentNullException(nameof(plainText));
            Byte[] toEncryptArray = Convert.FromBase64String(plainText);
            var rijndael = new RijndaelManaged()
            { Key = key_Array, Mode = System.Security.Cryptography.CipherMode.ECB, Padding = System.Security.Cryptography.PaddingMode.PKCS7, IV = iv_Array };
            System.Security.Cryptography.ICryptoTransform cTransform = rijndael.CreateDecryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            rijndael.Dispose();
            return System.Text.Encoding.UTF8.GetString(resultArray);
        }
        #endregion 
    }
}
